﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlindData
{
	public static class Generator
	{
		/// <summary>
		/// Return a next.
		/// </summary>
		/// <returns></returns>
		public static INext<T> Next<T>(this IGenerator generator)
		{
			return new Next<T>(generator);
		}

		public static IEnumerable<T> NextMany<T>(this IGenerator generator, Func<T> dataFactory, int count)
		{
			return generator.NextMany<T>(dataFactory, count, count);
		}

		public static IEnumerable<T> NextMany<T>(this IGenerator generator, Func<T> dataFactory, int minCount, int maxCount)
		{
			var count = generator.Next<int>().Between(minCount, maxCount);
			for (int i = 0; i < count; i++)
			{
				yield return dataFactory();
			}
		}

		#region enums

		/// <summary>
		/// Returns a random value from the enum of type T.
		/// </summary>
		/// <returns></returns>
		public static T NextEnum<T>(this IGenerator generator)
		{
			return (T)generator.NextEnum(typeof(T));
		}

		public static object NextEnum(this IGenerator generator, Type type)
		{
			if (type == null)
			{
				throw new ArgumentNullException("type");
			}

			if (!type.IsEnum)
			{
				throw new ArgumentException("type must be an enum");
			}
			var enumValues = Enum.GetValues(type);
			return enumValues.GetValue(generator.Next(enumValues.Length));
		}

		#endregion
	}
}
